<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-CH">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/niko/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/niko/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/niko/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Hexo, NexT" />








  <link rel="shortcut icon" type="image/x-icon" href="/niko/favicon.ico?v=5.1.1" />






<meta name="description" content="CoreSpotlight 学习#简介&amp;lt;CoreSpotlight&amp;gt;是 iOS9 / Xcode7 提供的一组新的API来帮助你建立起你的应用中的索引。CoreSpotlight是用来处理用户数据要使用CoreSpotlight首先要在工程-&amp;gt;Build Phases-&amp;gt;Link Binary With Libraries-&amp;gt;搜索CoreSpotlight引入Fram">
<meta property="og:type" content="article">
<meta property="og:title" content="NikoBlog">
<meta property="og:url" content="http://git.oschina.net/280772270/niko/2017/06/20/CoreSpotlight 学习/index.html">
<meta property="og:site_name" content="NikoBlog">
<meta property="og:description" content="CoreSpotlight 学习#简介&amp;lt;CoreSpotlight&amp;gt;是 iOS9 / Xcode7 提供的一组新的API来帮助你建立起你的应用中的索引。CoreSpotlight是用来处理用户数据要使用CoreSpotlight首先要在工程-&amp;gt;Build Phases-&amp;gt;Link Binary With Libraries-&amp;gt;搜索CoreSpotlight引入Fram">
<meta property="og:updated_time" content="2017-06-20T02:27:04.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="NikoBlog">
<meta name="twitter:description" content="CoreSpotlight 学习#简介&amp;lt;CoreSpotlight&amp;gt;是 iOS9 / Xcode7 提供的一组新的API来帮助你建立起你的应用中的索引。CoreSpotlight是用来处理用户数据要使用CoreSpotlight首先要在工程-&amp;gt;Build Phases-&amp;gt;Link Binary With Libraries-&amp;gt;搜索CoreSpotlight引入Fram">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/niko/',
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://git.oschina.net/280772270/niko/2017/06/20/CoreSpotlight 学习/"/>





  <title> | NikoBlog</title>
  














</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CH">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/niko/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">NikoBlog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">TecBlog</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/niko/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Startseite
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/niko/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archiv
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/niko/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            Tags
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://git.oschina.net/280772270/niko/niko/2017/06/20/CoreSpotlight 学习/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Yifei's">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/niko/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="NikoBlog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline"></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Veröffentlicht am</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2017-06-20T10:27:04+08:00">
                2017-06-20
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="CoreSpotlight-学习"><a href="#CoreSpotlight-学习" class="headerlink" title="CoreSpotlight 学习"></a>CoreSpotlight 学习</h1><p>#简介<br>&lt;CoreSpotlight&gt;是 <strong>iOS9 / Xcode7</strong> 提供的一组新的API来帮助你建立起你的应用中的索引。CoreSpotlight是用来处理用户数据<br>要使用CoreSpotlight首先要在工程-&gt;Build Phases-&gt;Link Binary With Libraries-&gt;搜索CoreSpotlight引入Framework</p>
<h1 id="搜索结果的初始化以及添加"><a href="#搜索结果的初始化以及添加" class="headerlink" title="搜索结果的初始化以及添加"></a>搜索结果的初始化以及添加</h1><blockquote>
<p><strong>CSSearchableItemAttributeSet</strong> ：声明CSSearchableItem包含的元数据。</p>
</blockquote>
<p>####CSSearchableItemAttributeSet 方法 <a href="http://e.hiphotos.baidu.com/image/pic/item/42166d224f4a20a42adfcc5c99529822730ed06c.jpg" target="_blank" rel="external">点击此处查看效果</a></p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/*应用内搜索object，想搜索到多少种界面就要创建多少个set ，每个set都要对应一个item*/</span></div><div class="line">    CSSearchableItemAttributeSet *firstSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:<span class="string">@"firstSet"</span>];</div><div class="line"><span class="comment">//标题</span></div><div class="line">    firstSet.title = <span class="string">@"搜索结果1"</span>;</div><div class="line"><span class="comment">//详细描述</span></div><div class="line">    firstSet.contentDescription = <span class="string">@"这是spotlight第一条搜索结果"</span>;</div><div class="line"><span class="comment">//关键字，搜索索引  经测试长的字符串spotlight可以自动模糊搜索，所以关键词</span></div><div class="line">    <span class="built_in">NSArray</span> *firstSeachKey = @[<span class="string">@"CoreSpotlight"</span>,<span class="string">@"结果"</span>,<span class="string">@"测试"</span>,<span class="string">@"搜索"</span>];</div><div class="line">    firstSet.contactKeywords = firstSeachKey;</div><div class="line"><span class="comment">//缩略图</span></div><div class="line">    firstSet.thumbnailData = [<span class="built_in">NSData</span> dataWithContentsOfURL:[<span class="built_in">NSURL</span> URLWithString:<span class="string">@"http://static0.tuicool.com/profile/05fcaf20abfa_1479439428.jpg"</span>]];</div></pre></td></tr></table></figure>
<blockquote>
<p><strong>CSSearchableItem</strong> 是CSSearchableItemAttributeSet的内容</p>
</blockquote>
<p>####CSSearchableItem 方法</p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//UniqueIdentifier每个搜索都有一个唯一标示，当用户点击搜索到得某个内容的时候，系统会调用代理方法，会将这个唯一标示传给你，以便让你确定是点击了哪一个，方便做页面跳转</span></div><div class="line"><span class="comment">//domainIdentifier搜索域标识，删除条目的时候调用的delegate会传过来这个值</span></div><div class="line">    CSSearchableItem *firstItem = [[CSSearchableItem alloc] initWithUniqueIdentifier:<span class="string">@"thirdItem"</span> domainIdentifier:<span class="string">@"third"</span> attributeSet:firstSet];</div></pre></td></tr></table></figure>
<blockquote>
<p>CSSearchableIndex 将初始好的items加入到搜索数据源里</p>
</blockquote>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[firstItem] completionHandler:^(<span class="built_in">NSError</span> * _Nullable error) &#123;</div><div class="line">        <span class="keyword">if</span> (error) &#123;</div><div class="line">            <span class="built_in">NSLog</span>(<span class="string">@"设置失败%@"</span>,error);</div><div class="line">        &#125;<span class="keyword">else</span>&#123;</div><div class="line">            <span class="built_in">NSLog</span>(<span class="string">@"设置成功"</span>);</div><div class="line">        &#125;</div><div class="line">    &#125;];</div></pre></td></tr></table></figure>
<h1 id="用户点击搜索结果的交互"><a href="#用户点击搜索结果的交互" class="headerlink" title="用户点击搜索结果的交互"></a>用户点击搜索结果的交互</h1><blockquote>
<p>类似remote消息，在”AppDelegate.m”里面实现以下方法</p>
</blockquote>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">- (<span class="built_in">BOOL</span>)application:(<span class="built_in">UIApplication</span> *)application continueUserActivity:(<span class="built_in">NSUserActivity</span> *)userActivity restorationHandler:(<span class="keyword">void</span> (^)(<span class="built_in">NSArray</span> * _Nullable))restorationHandler&#123;</div><div class="line">    </div><div class="line">    <span class="built_in">NSString</span> *dentifier  = userActivity.userInfo[<span class="string">@"kCSSearchableItemActivityIdentifier"</span>];</div><div class="line">    <span class="comment">//这里做一个封装好的来处理交互消息</span></div><div class="line">    <span class="keyword">return</span> <span class="literal">YES</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>#索引的删除</p>
<blockquote>
<p>CSSearchableIndex 提供了三种方法来删除索引</p>
</blockquote>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[CSSearchableIndex defaultSearchableIndex]<span class="comment">//系统SearchableIndex的单例</span></div><div class="line">- (<span class="keyword">void</span>)deleteSearchableItemsWithIdentifiers:(<span class="built_in">NSArray</span>&lt;<span class="built_in">NSString</span> *&gt; *)identifiers completionHandler:(<span class="keyword">void</span> (^ __<span class="keyword">nullable</span>)(<span class="built_in">NSError</span> * __<span class="keyword">nullable</span> error))completionHandler; </div><div class="line"></div><div class="line">- (<span class="keyword">void</span>)deleteSearchableItemsWithDomainIdentifiers:(<span class="built_in">NSArray</span>&lt;<span class="built_in">NSString</span> *&gt; *)domainIdentifiers completionHandler:(<span class="keyword">void</span> (^ __<span class="keyword">nullable</span>)(<span class="built_in">NSError</span> * __<span class="keyword">nullable</span> error))completionHandler;</div><div class="line"></div><div class="line"> - (<span class="keyword">void</span>)deleteAllSearchableItemsWithCompletionHandler:(<span class="keyword">void</span> (^ __<span class="keyword">nullable</span>)(<span class="built_in">NSError</span> * __<span class="keyword">nullable</span> error))completionHandler;</div></pre></td></tr></table></figure>
<p>#总结</p>
<blockquote>
<p>关于CSSearchableItemAttributeSet</p>
</blockquote>
<ol>
<li>contactKeywords的搜索索引，经过测试，<strong>一段长文字spotlight可以自动模糊匹配</strong>，比如将key设为“这是spotlight第一条搜索结果”输入“这是”、“zhes”、“第一条”···都会匹配搜索结果，key的长度没有进行验证</li>
<li>经测试缩略图的添加可以采用URL的方式，不用唤醒app，应该是系统自动根据URL读取其中的data</li>
<li>通过查看CSSearchableItemAttributeSet的头文件推测搜索结果支持国际化语言</li>
</ol>
<blockquote>
<p>关于UniqueIdentifier和domainIdentifier的设定</p>
</blockquote>
<ol>
<li>可以通过统一的前缀来设置关联，用于区分跳转以及删除和关闭某些敏感信息的搜索结果</li>
<li>通过UniqueIdentifier携带的信息来区分类别以及执行跳转至后的动作</li>
</ol>
<blockquote>
<p>关于搜索结果的插入</p>
</blockquote>
<ol>
<li>通过把玩新浪微博以及即刻脉脉之后发现，搜索的结果应该是之前打开app加载好的数据，相当于做了一个缓存，而之前没有加载的数据是不会搜索到的，所以猜测这个搜索是不能唤起app内部执行网络请求的。比如我之前微博没有关注“谢娜”，在spotlight搜索“谢娜”没有搜索结果，当我在微博里点进去谢娜的主页之后再出来搜索结果就出现了。</li>
<li>插入逻辑可以在数据请求成功后对不同的Model异步进行thumbData、title、description的处理</li>
</ol>
<blockquote>
<p>关于搜索结果的删除和关闭app在spotlight的显示</p>
</blockquote>
<ol>
<li>初步设想首先根据UniqueIdentifier和domainIdentifier对搜索结果进行分类，删除的时候调用CSSearchableIndex的方法统一删除，并且在插入搜索结果的工具类里进行Model屏蔽</li>
</ol>
<p>##参考文档<br><a href="http://www.cnblogs.com/CocoonJin/p/4703366.html" target="_blank" rel="external">iOS 9之应用内搜索(CoreSpotlight）API - CocoonJin的博客</a><br><a href="http://www.cnblogs.com/KingQiangzi/p/4861851.html" target="_blank" rel="external">iOS 9 应用内搜索(CoreSpotlight）的使用 - 魅风追影</a><br><a href="https://developer.apple.com/reference/corespotlight" target="_blank" rel="external">Apple Core Spotlight 文档</a></p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/niko/2017/06/20/5.ide的选择和配置/" rel="prev" title="">
                 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            Inhaltsverzeichnis
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            Übersicht
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/niko/images/avatar.gif"
               alt="Yifei's" />
          <p class="site-author-name" itemprop="name">Yifei's</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/niko/archives/">
                <span class="site-state-item-count">7</span>
                <span class="site-state-item-name">Artikel</span>
              </a>
            </div>
          

          

          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#CoreSpotlight-学习"><span class="nav-number">1.</span> <span class="nav-text">CoreSpotlight 学习</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#搜索结果的初始化以及添加"><span class="nav-number">2.</span> <span class="nav-text">搜索结果的初始化以及添加</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#用户点击搜索结果的交互"><span class="nav-number">3.</span> <span class="nav-text">用户点击搜索结果的交互</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Yifei's</span>
</div>


<div class="powered-by">
  Erstellt mit  <a class="theme-link" href="https://hexo.io">Hexo</a>
</div>

<div class="theme-info">
  Theme -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/niko/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/niko/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/niko/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/niko/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/niko/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/niko/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/niko/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/niko/js/src/motion.js?v=5.1.1"></script>



  
  

  
  <script type="text/javascript" src="/niko/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/niko/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/niko/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  






  





  

  

  

  

  

  

</body>
</html>
